package com.example.myblogserver.service.impl;

import com.example.myblogserver.common.utils.RedisUtil;
import com.example.myblogserver.dto.AdminLoginDTO;

import com.example.myblogserver.mapper.UsersAccountMapper;
import com.example.myblogserver.model.UsersAccount;
import com.example.myblogserver.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private UsersAccountMapper usersAccountMapper;
    @Autowired
    private RedisUtil redisUtil;
     @Override
    public UsersAccount login(AdminLoginDTO adminLoginDTO)
     {
          String  userEmail = adminLoginDTO.getUserEmail();
          String userPassword = adminLoginDTO.getUserPassword();
          String code = adminLoginDTO.getCode();
          UsersAccount usersAccount = usersAccountMapper.selectByUserEmail( userEmail);
         if (usersAccount == null) {
             //账号不存在
             throw new  RuntimeException("账号不存在");
         }
         if (!usersAccount.getUserPassword().equals(userPassword)) {
             //密码错误
             throw new  RuntimeException("密码错误");
         }
         if (!redisUtil.getCode("email:code:" +userEmail).equals(code)) {
             //验证码错误
             throw new  RuntimeException("验证码错误");
         }
          return usersAccount;

     }


}
